<template>
<div>
    <div>
    <el-card class="box-card" style="margin-left: auto; margin-right: auto;">
        <div slot="header" class="clearfix">
            <span>活动管理</span>
        </div>
        <div>
          <span style=" float: left; margin-top: 10px;">活动主题</span>
          <el-input style="float: left; width: 20%; margin-bottom: 20px;  margin-left: 20px;" v-model="activityTheme"></el-input>
          <el-button type="primary" icon="el-icon-circle-plus" style="float: right; " @click="activitySelect(activityTheme)">查询</el-button>
        </div>
    </el-card>
    <el-card class="box-card" style="margin-left: auto; margin-right: auto;">
        <div slot="header" class="clearfix">
            <span>活动列表</span>
            <el-button type="primary" icon="el-icon-circle-plus" style="float: right; " @click="Add(),dialogFormVisible1 = true">新建</el-button>
        </div>
        <div>
            <el-table
            :data="InfoList"
            style="width: 100%">
            <el-table-column
                prop="communityName"
                label="小区名字"
                width="150">
            </el-table-column>
            <el-table-column
                prop="activityTheme"
                label="活动主题"
                width="150">
            </el-table-column>
            <el-table-column
                prop="theAuthor"
                label="作者"
                width="150">
            </el-table-column>
            <el-table-column
                prop="content"
                label="内容"
                width="150">
            </el-table-column>
            <el-table-column
                prop="startTime"
                label="预约日期"
                width="150">
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="200">
                <template slot-scope="scope">
                    <!-- <el-button  type="text" size="small">审核</el-button> -->
                    <el-button  type="text" size="small" @click="Update(scope.row),dialogFormVisible= true">编辑</el-button>
                    <el-button type="text" size="small" @click="repairDelect(scope.row.communityActivitiesId)">删除</el-button>
                </template>
            </el-table-column>
            </el-table>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[2, 4, 6, 8]"
              :page-size="2"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pageTotal">
            </el-pagination>
        </div>
    </el-card>
  </div>
  <div>
        <el-dialog title="新增" :visible.sync="dialogFormVisible1">
            <el-form :model="ruleForm" status-icon  ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="选择小区" >
                <el-select v-model="ruleForm.communityId" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.communityId"
                    :label="item.communityName"
                    :value="item.communityId">
                  </el-option>
                </el-select>
                </el-form-item>
                <el-form-item label="活动标题" >
                <el-input v-model="ruleForm.activityTheme"></el-input>
                </el-form-item>
                <el-form-item label="作者" >
                <el-input v-model="ruleForm.theAuthor"></el-input>
                </el-form-item>
                <el-form-item label="详细内容" >
                <el-input v-model="ruleForm.content"></el-input>
                </el-form-item>
                <el-form-item label="活动预约时间" >
                <div class="block">
                  <el-date-picker
                    v-model="ruleForm.startTime"
                    type="datetime"
                    placeholder="选择日期时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                   >
                  </el-date-picker>
                </div>
                </el-form-item>
                <el-form-item >
                <el-button type="primary" @click="repairAdd('ruleForm')">提交</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <el-dialog title="编辑" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" status-icon  ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="选择小区" >
                <el-select v-model="ruleForm.communityId" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.communityId"
                    :label="item.communityName"
                    :value="item.communityId">
                    </el-option>
                  </el-select>
                  </el-form-item>
                  <el-form-item label="活动标题" >
                  <el-input v-model="ruleForm.activityTheme"></el-input>
                  </el-form-item>
                  <el-form-item label="作者" >
                  <el-input v-model="ruleForm.theAuthor"></el-input>
                  </el-form-item>
                  <el-form-item label="详细内容" >
                  <el-input v-model="ruleForm.content"></el-input>
                  </el-form-item>
                  <el-form-item label="活动预约时间" >
                  <div class="block">
                  <el-date-picker
                    v-model="ruleForm.startTime"
                    type="datetime"
                    placeholder="选择日期时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                   >
                  </el-date-picker>
                </div>
                </el-form-item>
                  <el-form-item >
                  <el-button type="primary" @click="repairUpdate('ruleForm')">提交</el-button>
                  </el-form-item>
            </el-form>
        </el-dialog>
  </div>
</div>
</template>

<script>
export default {
  data() {
        return {
          tableData: [],
          InfoList:[],
          input: '',
          dialogFormVisible: false,
          dialogFormVisible1: false,
          ruleForm:{
            communityActivitiesId:'',
            activityTheme:'',
            theAuthor:'',
            content:'',
            startTime:'',
            communityName:'',
            communityId:''
          },
          ruleForm1:{
            activityTheme:'',
          },
          pageTotal: 0, // 总数
          pageNum: 1, // 页数
          pageSize: 2, // 条数
          currentPage: 1, // 分页中当前页变量
          options: [
            
          ],
          value: '',
          activityTheme:'',
        }
    },
    methods: {
          chongzhi(){
            this.ruleForm1.activityTheme="";
          },
          middle(){
            this.$axios.get("http://localhost:9000/serverchy/Activity/middle").then(response=>{
                  this.options=response.data.data.list
              }
              ).catch(e=>{
              })
          },
          repairDelect(communityActivitiesId){
              this.$confirm('此操作将永久删除用户信息, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
              }).then(() => {
              this.$axios.get("http://localhost:9000/serverchy/Activity/activityDelect",{
                  params: {
                  communityActivitiesId: communityActivitiesId
                  }
              }).then(response=>{
                  this.activitySelect();
                  this.$message({
                  message: '删除成功',
                  type: 'success'
                  })
              }).catch(e=>{  
              })
              }).catch(e=>{   
              })
        },
        Update(data){
              this.middle();
              this.ruleForm.communityActivitiesId=data.communityActivitiesId
              this.ruleForm.communityId=data.communityId
              this.ruleForm.activityTheme= data.activityTheme
              this.ruleForm.theAuthor= data.theAuthor
              this.ruleForm.content=data.content
              this.ruleForm.startTime=data.startTime
              this.ruleForm.communityName=data.communityName
          },
        Add(){
            this.ruleForm.communityId=''
            this.ruleForm.activityTheme= ''
            this.ruleForm.theAuthor= ''
            this.ruleForm.content=''
            this.ruleForm.startTime=''
            this.middle();
        },
        repairAdd(){
              console.log(this.communityId);
              this.$axios.post("http://localhost:9000/serverchy/Activity/activityAdd",{
              communityId:this.ruleForm.communityId,
              activityTheme: this.ruleForm.activityTheme,
              theAuthor: this.ruleForm.theAuthor,
              content: this.ruleForm.content,
              startTime:this.ruleForm.startTime,
            }).then(response=>{
                this.activitySelect();
                this.dialogFormVisible1=false
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
            }).catch(e=>{
                this.$message.error('添加失败')
            })
        },
        repairUpdate(){
              this.$axios.post("http://localhost:9000/serverchy/Activity/activityUpdate",{
              communityActivitiesId:this.ruleForm.communityActivitiesId,
              communityId:this.ruleForm.communityId,
              activityTheme: this.ruleForm.activityTheme,
              theAuthor: this.ruleForm.theAuthor,
              content: this.ruleForm.content,
              startTime:this.ruleForm.startTime,
            }).then(response=>{
                this.activitySelect();
                this.dialogFormVisible=false
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
            }).catch(e=>{
                this.$message.error('修改失败')
            })
        },
        handleSizeChange(val) {
          this.pageSize = val
          this.activitySelect()
        },
        handleCurrentChange(val) {
          this.pageNum =val
          this.activitySelect()
        },
        activitySelect() {
            // 获取所有用户信息
            this.$axios.get("http://localhost:9000/serverchy/Activity/activitySelect",{
              params:{
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                activityTheme:this.activityTheme
              }
          }).then((res) => {
              this.InfoList = res.data.list
              this.pageTotal = res.data.total
            });
        },
    },
    mounted() {
      this.activitySelect()
    }
}
</script>

<style>
    .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 1136px;
  }
</style>